//
//  UIImage+Creation.m
//  YongQi
//
//  Created by dqb on 15/2/4.
//  Copyright (c) 2015年 dqb. All rights reserved.
//

#import "UIImage+Creation.h"

@implementation UIImage (Creation)
+ (UIImage *)createImageWithColor:(UIColor *)color andSize:(CGSize)size cornerRadius:(CGFloat)cornerRadius{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, 0, UIScreen.mainScreen.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    [path addClip];
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size{
    return [UIImage createImageWithColor:color andSize:size cornerRadius:0];
  
}


- (UIImage *)normalizedImage{
    if (self) {
        if (self.imageOrientation == UIImageOrientationUp) return self;
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        [self drawInRect:(CGRect){0, 0, self.size}];
        UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return normalizedImage;
    }else{
        return nil;
    }
}

@end
